class Base::DepartmentsController < ApplicationController
  before_action :set_base_department, only: [:show, :edit, :update, :destroy]

  # GET /base/departments
  # GET /base/departments.json
  def index
    @base_departments = Base::Department.this.roots.includes(:children)
  end

  # GET /base/departments/1
  # GET /base/departments/1.json
  def show
  end

  # GET /base/departments/new
  def new
    @html_title = '新增部门'
    @base_department = Base::Department.new
    render layout: 'modal'
  end

  # GET /base/departments/1/edit
  def edit
    @html_title = '部门编辑'
    render layout: 'modal'
  end

  # POST /base/departments
  # POST /base/departments.json
  def create
    @base_department = Base::Department.this.new(base_department_params)

    if @base_department.save
      show_js_notice('创建成功！')
    else
      error_notice(@base_department)
    end
  end

  # PATCH/PUT /base/departments/1
  # PATCH/PUT /base/departments/1.json
  def update
    if @base_department.update(base_department_params)
      show_js_notice('修改成功！')
    else
      error_notice(@base_department)
    end
  end

  # DELETE /base/departments/1
  # DELETE /base/departments/1.json
  def destroy
    @base_department.destroy
    show_js_notice('删除成功！')
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_base_department
      @base_department = Base::Department.this.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def base_department_params
      params.fetch(:base_department).permit(:id, :name, :parent_id)
    end
end
